package com.atguigu.gmall.order.config.mq;

import com.atguigu.gmall.common.constant.RabbitConst;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

/**
 * @Author:lyd
 * @Date:2023/9/5 20:19
 * @Version:1.0
 * @Description 创建订单模块中需要的所有交换机、队列，以及它们的绑定关系
 **/
@Configuration
public class OrderMqComponentConfig {


    /**
     * String name,交换机的名字
     * boolean durable, 是否持久化
     * boolean autoDelete, 是否自动删除
     * Map<String, Object> arguments 设置参数
     *
     * @return
     */
    @Bean
    public Exchange orderEventExchange() {
        return new TopicExchange(RabbitConst.EXCHANGE_ORDER_EVENT,
                true,
                false,
                null);
    }


    /**
     * String name, 队列名字
     * boolean durable, 是否持久化
     * boolean exclusive,
     * 排他：这个队列只能有一个消费者监听; 应该所有消费者合起来负载均衡的消费队列所有内容。
     * boolean autoDelete,是否自动删除
     *
     * @return
     * @Nullable Map<String, Object> arguments设置参数
     */


    //延时队列
    @Bean
    public Queue orderDelayQueue() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("x-message-ttl", 1000 * 60 * 30);//消息过期时间，单位是毫秒
        map.put("x-dead-letter-exchange", RabbitConst.EXCHANGE_ORDER_EVENT);
        map.put("x-dead-letter-routing-key", RabbitConst.RK_ORDER_TIMEOUT);
        return new Queue(RabbitConst.QUEUE_ORDER_CREATE,
                true,
                false,
                false,
                map);
    }


    /**
     * 延时队列和交换机绑定
     * String destination, 目的地
     * DestinationType destinationType, 目的地类型
     * String exchange, 交换机
     * String routingKey, 路由key
     *
     * @return
     * @Nullable Map<String, Object> arguments 参数
     */
    @Bean
    public Binding orderDelayBinding() {
        return new Binding(RabbitConst.QUEUE_ORDER_CREATE,
                Binding.DestinationType.QUEUE,
                RabbitConst.EXCHANGE_ORDER_EVENT,
                RabbitConst.RK_ORDER_CREATE,
                null
        );
    }

    /**
     * 死信队列
     *
     * @return
     */
    @Bean
    public Queue orderDeadQueue() {
        return new Queue(RabbitConst.QUEUE_ORDER_TIMEOUT,
                true,
                false,
                false,
                null);
    }


    /**
     * 死信队列和交换机绑定
     *
     * @return
     */
    @Bean
    public Binding orderDeadBiding() {
        return new Binding(RabbitConst.QUEUE_ORDER_TIMEOUT,
                Binding.DestinationType.QUEUE,
                RabbitConst.EXCHANGE_ORDER_EVENT,
                RabbitConst.RK_ORDER_TIMEOUT,
                null);
    }

    @Bean
    public Queue orderPayedQueue() {

        return new Queue(
                RabbitConst.QUEUE_ORDER_PAYED,
                true,
                false,
                false);
    }

    @Bean
    public Binding orderPayedBinding() {
        return new Binding(
                RabbitConst.QUEUE_ORDER_PAYED,
                Binding.DestinationType.QUEUE,
                RabbitConst.EXCHANGE_ORDER_EVENT,
                RabbitConst.RK_ORDER_PAYED,
                null
        );
    }
}